package com.hontech.libratest.activity.fragment.sub

import android.view.View
import android.widget.Button
import com.hontech.libratest.R
import com.hontech.libratest.activity.window.showProg
import com.hontech.libratest.conn.getRasterPick
import com.hontech.libratest.conn.setRasterPick
import com.hontech.libratest.utils.onClick

class RasterPickSub (view: View) {

    private val btnRasterOn = view.findViewById<Button>(R.id.id_main_btn_raster_pick_on)
    private val btnRasterOff = view.findViewById<Button>(R.id.id_main_btn_raster_pick_off)
    private val btnRasterGet = view.findViewById<Button>(R.id.id_main_btn_raster_pick_get)

    init {
        btnRasterOn.onClick(::onClickOn)
        btnRasterOff.onClick(::onClickOff)
        btnRasterGet.onClick(::onClickGet)
    }

    private suspend fun onClickOn() {
        val w = showProg(btnRasterGet, "启用光栅取货")
        try {
            setRasterPick(0)
            w.success("启用成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickOff() {
        val w = showProg(btnRasterGet, "禁用光栅取货")
        try {
            setRasterPick(1)
            w.success("禁用成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickGet() {
        val w = showProg(btnRasterGet, "获取光栅取货状态")
        try {
            val value = getRasterPick()
            if (value == 0) {
                w.success("光栅取货启用")
            } else {
                w.success("光栅取货禁用")
            }
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }
}